#include "../include/header.h"

int main(int argc,char*argv[]){

    int pipe_read = open("1.pipe", O_RDONLY);
    int pipe_write = open("2.pipe", O_WRONLY);

    fd_set set;
    FD_ZERO(&set);

    while(1){

        FD_SET(STDIN_FILENO, &set);
        FD_SET(pipe_read, &set);
        select(10, &set, NULL, NULL, NULL);

        if(FD_ISSET(STDIN_FILENO, &set)){
            char buf[60] = {0};
            bzero(buf, sizeof(buf));
            read(STDIN_FILENO, buf, sizeof(buf));
            write(pipe_write, buf, sizeof(buf));
        }

        if(FD_ISSET(pipe_read, &set)){
            char buf[60] = {0};
            bzero(buf, sizeof(buf));
            int res = read(pipe_read, buf, sizeof(buf));
            if(res == 0){
                printf("对端关闭了管道 \n");
                break;
            }
            printf("A: %s \n", buf);
        }
    }

    close(pipe_read);
    close(pipe_write);

    return 0;
}

